/*********************************************************************************
  *FileName:  nsuc16xx_gpio.c
  *Version:   V1.0
  *Date:      2023-09-21
****************************************Includes***********************************/
#include "nsuc16xx_gpio.h"

/***************************************Variables***********************************/

/***************************************Functions***********************************/
/**
 * @brief  Sets the selected data port bits.
 * @param  GPIO_Pin: specifies the port bits to be written.
 *   This parameter can be any combination of GPIO_Pin_x where x can be (0..10).
 * @retval None
 */
void GPIO_SetBits(uint32_t GPIO_Pin)
{
    GPIO->BSR = GPIO_Pin;
}

/**
 * @brief  Clears the selected data port bits.
 * @param  GPIO_Pin: specifies the port bits to be written.
 *   This parameter can be any combination of GPIO_Pin_x where x can be (0..10).
 * @retval None
 */
void GPIO_ResetBits(uint32_t GPIO_Pin)
{
    GPIO_Pin <<= 16;
    GPIO->BSR = GPIO_Pin;
}

/**
 * @brief  Reads the specified input port pin.
 * @param  GPIO_Pin:  specifies the port bit to read.
 *   This parameter can be GPIO_Pin_x where x can be (0..10).
 * @retval The input port pin value.
 */
uint8_t GPIO_ReadInputDataBit(uint32_t GPIO_Pin)
{
    uint8_t bitstatus = 0x00;

    if ((GPIO->PDI & GPIO_Pin) != (uint32_t)Bit_RESET)
    {
        bitstatus = (uint8_t)Bit_SET;
    }
    else
    {
        bitstatus = (uint8_t)Bit_RESET;
    }
    return bitstatus;
}

/**
 * @brief  Reads the specified output data port bit.
 * @param  GPIO_Pin:  specifies the port bit to read.
 *   This parameter can be GPIO_Pin_x where x can be (0..10).
 * @retval The output port pin value.
 */
uint8_t GPIO_ReadOutputDataBit(uint32_t GPIO_Pin)
{
    uint8_t bitstatus = 0x00;

    if ((GPIO->PDO & GPIO_Pin) != (uint32_t)Bit_RESET)
    {
        bitstatus = (uint8_t)Bit_SET;
    }
    else
    {
        bitstatus = (uint8_t)Bit_RESET;
    }
    return bitstatus;
}

/**
 * @brief  GPIO Alternate functions configuration function.
 * @param  GPIO_PinSource: This parameter can be GPIO_PinSourcex where x can be (0...5, 8...10).
 * @param  GPIO_AFSelection: Alternate functions.
 * @retval None.
 */
void GPIO_PinAFConfig(uint16_t GPIO_PinSource, uint8_t GPIO_AF)
{
    uint32_t temp = 0x00;
    uint32_t temp_2 = 0x00;

    if (GPIO_PinSource <= 7)
    {
        temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
        GPIO->MXR &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)GPIO_PinSource & (uint32_t)0x07) * 4));
        temp_2 = GPIO->MXR | temp;
        GPIO->MXR = temp_2;
    }
    else
    {
        temp = ((uint32_t)(GPIO_AF) << ((uint32_t)((uint32_t)(GPIO_PinSource - 8) & (uint32_t)0x07) * 4));
        GPIO->MYR &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)(GPIO_PinSource - 8) & (uint32_t)0x07) * 4));
        temp_2 = GPIO->MYR | temp;
        GPIO->MYR = temp_2;
    }
}

/**
 * @brief  GPIO pull-down configuration function.
 * @param  GPIO_Pin: specifies the port bits to be config.
 *   This parameter can be any combination of GPIO_Pin_x where x can be (0..10).
 * @param  GPIO_PuPd: pull-down config.
 * @retval None.
 */
void GPIO_PuPdConfig(uint32_t GPIO_Pin, GPIOPuPd_TypeDef GPIO_PuPd)
{
    if (GPIO_PuPd == GPIO_PuPd_NOPULL)
    {
        GPIO->PUR &= ~GPIO_Pin;
        GPIO->PDR &= ~GPIO_Pin;
    }
    else if (GPIO_PuPd == GPIO_PuPd_UP)
    {
        GPIO->PUR |= GPIO_Pin;
        GPIO->PDR &= ~GPIO_Pin;
    }
    else if (GPIO_PuPd == GPIO_PuPd_DOWN)
    {
        GPIO->PUR &= ~GPIO_Pin;
        GPIO->PDR |= GPIO_Pin;
    }
    else
    {
    }
}

/**
 * @brief  GPIO output type configuration function.
 * @param  GPIO_Pin: specifies the port bits to be config.
 *   This parameter can be any combination of GPIO_Pin_x where x can be (0..10).
 * @param  GPIO_OType: Output type.
 * @retval None.
 */
void GPIO_OutTypeConfig(uint32_t GPIO_Pin, GPIOOType_TypeDef GPIO_OType)
{
    if (GPIO_OType == GPIO_OType_PP)
    {
        GPIO->ODR &= ~GPIO_Pin;
    }
    else
    {
        GPIO->ODR |= GPIO_Pin;
    }
}

/**
 * @brief  GPIO Input debounce configuration function.
 * @param  GPIO_Pin: specifies the port bits to be config.
 *   This parameter can be any combination of GPIO_Pin_x where x can be (0..10).
 * @param  NewState: Enable or disable.
 * @retval None.
 */
void GPIO_DebounceConfig(uint32_t GPIO_Pin, FunctionalState NewState)
{
    if (NewState == ENABLE)
    {
        GPIO->DER |= GPIO_Pin;
    }
    else
    {
        GPIO->DER &= ~GPIO_Pin;
    }
}

/**
 * @brief  GPIO Input debounce time configuration function.
 * @param  DT: debounce time(0.25us x DT).
 * @retval None.
 */
void GPIO_DebounceTimeConfig(uint32_t DT)
{
    GPIO->DTR = (DT & 0x03);
}

/***************************************END OF FILE*********************************/
